package parser_antlr4.mysql;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.sql.parser.Token;
import domain.Table;
import parser_antlr4.Parser;

import java.util.ArrayList;
import java.util.List;

public class MySQLSQLParser implements Parser {

  public static void main(String[] args) {
    String sql1 = "CREATE TABLE `form_ac_cwgxrzxxndr11` ( `creditRating` varchar(256) DEFAULT NULL COMMENT '国企信用等级', `initialFinancingAmount` decimal(20,2) DEFAULT NULL COMMENT '本年初融资总额', `adjustedFinancingAmount` decimal(20,2) DEFAULT NULL COMMENT '调整融资总额', `revisedFinancingAmount` decimal(20,2) DEFAULT NULL COMMENT '调整后融资总额', `initialProjectFund` decimal(20,2) DEFAULT NULL COMMENT '年初融资资金用途-项目', `initialWorkingCapital` decimal(20,2) DEFAULT NULL COMMENT '年初融资资金用途-补充流动资金', `initialDebtRepayment` decimal(20,2) DEFAULT NULL COMMENT '年初融资资金用途-偿还债务', `revisedProjectFund` decimal(20,2) DEFAULT NULL COMMENT '调整后融资资金用途-项目', `revisedWorkingCapital` decimal(20,2) DEFAULT NULL COMMENT '调整后融资资金用途-补充流动资金', `revisedDebtRepayment` decimal(20,2) DEFAULT NULL COMMENT '调整后融资资金用途-偿还债务', `impactOnDebtAndFinance` varchar(6000) DEFAULT NULL COMMENT '实施年度融资计划对本企业债务结构、财务状况的影响', `USCI` varchar(256) DEFAULT NULL COMMENT '企业统一社会信用代码', `enterpriseFullName` varchar(256) DEFAULT NULL COMMENT '企业全称', `businessCode` varchar(256) DEFAULT NULL COMMENT '业务编码', `id` bigint(255) NOT NULL AUTO_INCREMENT COMMENT '主键', `task_id` bigint(255) DEFAULT NULL COMMENT '任务编号', `group_id` bigint(255) DEFAULT NULL COMMENT '分组编号', `enterprise_id` bigint(255) DEFAULT NULL COMMENT '企业编号', `is_del` tinyint(255) DEFAULT NULL COMMENT '是否删除,0否1是(逻辑删除)', `create_by` varchar(32) DEFAULT NULL COMMENT '创建人', `update_by` varchar(32) DEFAULT NULL COMMENT '修改人', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_time` datetime DEFAULT NULL COMMENT '修改时间', `data_status` char(1) DEFAULT NULL COMMENT '数据状态，字典编码: data_status', `report_type` varchar(4) DEFAULT NULL COMMENT '填报方式', PRIMARY KEY (`id`) ) AUTO_INCREMENT = 7 AUTO_INCREMENT_MODE 'ORDER' DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE ENABLE_MACRO_BLOCK_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0 COMMENT = '财务共享-融资信息-年度融资计划';";
    String sql2 = "CREATE TABLE `form_ac_cwgxrzxxndrzjh22` ( `creditRating` varchar(256) DEFAULT NULL COMMENT '国企信用等级', `initialFinancingAmount` decimal(20,2) DEFAULT NULL COMMENT '本年初融资总额', `adjustedFinancingAmount` decimal(20,2) DEFAULT NULL COMMENT '调整融资总额', `revisedFinancingAmount` decimal(20,2) DEFAULT NULL COMMENT '调整后融资总额', `initialProjectFund` decimal(20,2) DEFAULT NULL COMMENT '年初融资资金用途-项目', `initialWorkingCapital` decimal(20,2) DEFAULT NULL COMMENT '年初融资资金用途-补充流动资金', `initialDebtRepayment` decimal(20,2) DEFAULT NULL COMMENT '年初融资资金用途-偿还债务', `revisedProjectFund` decimal(20,2) DEFAULT NULL COMMENT '调整后融资资金用途-项目', `revisedWorkingCapital` decimal(20,2) DEFAULT NULL COMMENT '调整后融资资金用途-补充流动资金', `revisedDebtRepayment` decimal(20,2) DEFAULT NULL COMMENT '调整后融资资金用途-偿还债务', `impactOnDebtAndFinance` varchar(6000) DEFAULT NULL COMMENT '实施年度融资计划对本企业债务结构、财务状况的影响', `USCI` varchar(256) DEFAULT NULL COMMENT '企业统一社会信用代码', `enterpriseFullName` varchar(256) DEFAULT NULL COMMENT '企业全称', `businessCode` varchar(256) DEFAULT NULL COMMENT '业务编码', `id` bigint(255) NOT NULL AUTO_INCREMENT COMMENT '主键', `task_id` bigint(255) DEFAULT NULL COMMENT '任务编号', `group_id` bigint(255) DEFAULT NULL COMMENT '分组编号', `enterprise_id` bigint(255) DEFAULT NULL COMMENT '企业编号', `is_del` tinyint(255) DEFAULT NULL COMMENT '是否删除,0否1是(逻辑删除)', `create_by` varchar(32) DEFAULT NULL COMMENT '创建人', `update_by` varchar(32) DEFAULT NULL COMMENT '修改人', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_time` datetime DEFAULT NULL COMMENT '修改时间', `data_status` char(1) DEFAULT NULL COMMENT '数据状态，字典编码: data_status', `report_type` varchar(4) DEFAULT NULL COMMENT '填报方式', PRIMARY KEY (`id`) ) ORGANIZATION INDEX AUTO_INCREMENT = 7 AUTO_INCREMENT_MODE = 'ORDER' DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE ENABLE_MACRO_BLOCK_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0 COMMENT = '财务共享-融资信息-年度融资计划';";
    String sql3 = "alter table `form_ac_cwgxrzxxndrzjh22` add name int(11) default null";
    String sql = sql1 + sql2 + sql3;


    SQLStatementParser parser = new MySQLCreateParser(sql);
    List<SQLStatement> stmtList = new ArrayList<SQLStatement>();
    parser.parseStatementList(stmtList, -1, null);
    if (parser.getLexer().token() != Token.EOF) {
      throw new ParserException("syntax error : " + parser.getLexer().info());
    }

    stmtList.stream().forEach(sqlStatement -> {
      if (!(sqlStatement instanceof MySqlCreateTableStatement)) {
        throw new ParserException("非MySQL create语句");
      }
    });
  }

  @Override
  public List<Table> parseCreateSQL(String createDDL) throws Exception {
    return null;
  }
}
